package com.lmpop.frame.dao;

import java.sql.SQLException;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.lmpop.frame.util.InitProject;
import com.ibatis.sqlmap.client.SqlMapClient;

public class IbatisDbmImpl
  implements DbmInterf
{
  protected SqlMapClient sqlMap;
  private static final IbatisDbmImpl single = new IbatisDbmImpl();

  public IbatisDbmImpl()
  {
    initSqlMapClient();
  }

  public static IbatisDbmImpl getInstance()
  {
    return single;
  }

  public void initSqlMapClient() {
	  this.sqlMap = InitProject.initMap();
  }

  public void update(String arg1, Object arg2)
  {
    try {
      this.sqlMap.update(arg1, arg2);
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
  }

  public void updateSql(String arg1, Object arg2) {
    start();
    update(arg1, arg2);
    commit();
    end();
  }

  public List getList(String arg1, Object arg2) {
    List infoList = null;
    try {
      infoList = this.sqlMap.queryForList(arg1, arg2);
    } catch (SQLException e) {
      e.printStackTrace();
    }
    return infoList;
  }

  public void start()
  {
    try {
      this.sqlMap.startTransaction();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public void commit() {
    try {
      this.sqlMap.commitTransaction();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public void end() {
    try {
      this.sqlMap.endTransaction();
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    IbatisDbmImpl idil = new IbatisDbmImpl();
    List list = idil.getList("getAllTs_categoryByType", "2");
    Map map = null;
    for (int i = 0; i < list.size(); i++) {
      map = (Hashtable)list.get(i);
      idil.update("updateTs_categorypyKey", map.get("cat_id").toString());
    }
  }
}